package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.dto.AddDishDTO;
import com.itheima.pojo.PageBean;
import com.itheima.service.DishService;
import com.itheima.vo.DishAndCategoryVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 小帅蓝
 * @Date 2022/6/16 18:56
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Resource
    private DishService dishService;

    @GetMapping("/page")
    public R<PageBean> query(String name, Integer page, Integer pageSize, String type) {
        List<DishAndCategoryVO> dishVOList = dishService.query(name, page, pageSize, type);
        Integer total = dishService.getCount(name);
        return R.success(new PageBean(dishVOList, total, pageSize, page));
    }

    @GetMapping("/list")
    public R<List<DishAndCategoryVO>> selectList(String categoryId) {
        return R.success(dishService.selectList(categoryId, null));
    }

    @GetMapping("/{id}")
    public R<DishAndCategoryVO> selectByDishId(@PathVariable("id") String id) {
        DishAndCategoryVO dishAndCategoryVO = dishService.queryById(id);
        return R.success(dishAndCategoryVO);
    }

    @PostMapping
    public R<String> addDish(@RequestBody AddDishDTO addDishDTO) {
        return dishService.addDish(addDishDTO);
    }

    @PutMapping
    public R<String> updateDish(@RequestBody AddDishDTO addDishDTO) {
        return dishService.updateDish(addDishDTO);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam String ids) {
        boolean result = dishService.deleteDish(ids);
        return result ? R.success("成功") : R.error("失败");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam String ids, @PathVariable Integer status) {
        boolean result = dishService.updateStatus(ids, status);
        return result ? R.success("修改状态成功！") : R.error("修改状态失败！");
    }

}
